﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Ewell.Interface.Common.Utils
{
    /// <summary>
    /// 扩展动态调用动态库
    /// </summary>
    public class KernelExtend
    {
        /// <summary>
        /// 创建调用动态库委托
        /// </summary>
        /// <typeparam name="TDelegate"></typeparam>
        /// <param name="dllPath"></param>
        /// <param name="methodName"></param>
        /// <returns></returns>
        public static TDelegate CreateDelInstance<TDelegate>(string dllPath, string methodName)
        {
            int hModule = KernelHelper.LoadLibrary(dllPath);
            if (hModule == 0)
            {
                return default(TDelegate);
            }
            IntPtr intPtr = KernelHelper.GetProcAddress(hModule, methodName);
            return Marshal.GetDelegateForFunctionPointer<TDelegate>(intPtr);
        }
    }
}
